package agentV1

import (
	"github.com/gin-gonic/gin"
	"github.com/spf13/cast"
	"hc_server/pkg/app"
	"hc_server/routers/service/codeService"
	"hc_server/routers/service/salesmenService"
	"hc_server/runtime/e"
)

type SalesmenRegisterParam struct {
	Name    string      `json:"name" `    //业务员姓名
	Mobile  interface{} `json:"mobile" `  //业务员手机号码
	AgentId interface{} `json:"agentId" ` //代理商id
}

// SalesmenRegister 业务员注册
func SalesmenRegister(c *gin.Context) {
	appG := app.Gin{C: c}
	var (
		param   SalesmenRegisterParam
		errCode int
	)

	_, errCode = app.BindJsonAndValid(c, &param)
	if errCode != e.SUCCESS {
		appG.ResponseError(e.CodeMap[errCode], nil)
		return
	}

	if len(param.Name) == 0 {
		appG.ResponseError("缺少参数name", nil)
		return
	}

	if len(cast.ToString(param.AgentId)) == 0 {
		appG.ResponseError("缺少参数agentId", nil)
		return
	}

	if len(cast.ToString(param.Mobile)) == 0 {
		appG.ResponseError("缺少参数mobile", nil)
		return
	}

	salesmenService := salesmenService.Salesmen{
		Name:    param.Name,
		Mobile:  param.Mobile,
		AgentId: cast.ToString(param.AgentId),
	}

	err := salesmenService.SalesmenRegister()
	if err != nil {
		appG.ResponseError(err.Error(), nil)
		return
	}

	appG.ResponseSuccess("注册成功", nil)
}

type AgentMerchantSalesmenListParam struct {
	Id       any    `json:"id" `
	PageNum  any    `json:"pageNum"`
	PageSize any    `json:"pageSize"`
	Keyword  string `json:"keyword"`
}

// SalesmenNormaList 业务员列表
func SalesmenNormaList(c *gin.Context) {
	appG := app.Gin{C: c}
	var (
		param   AgentMerchantSalesmenListParam
		errCode int
	)

	_, errCode = app.BindJsonAndValid(c, &param)
	if errCode != e.SUCCESS {
		appG.ResponseError(e.CodeMap[errCode], nil)
		return
	}

	if len(cast.ToString(param.Id)) == 0 {
		appG.ResponseError("缺少参数id", nil)
		return
	}

	salesmenService := salesmenService.Salesmen{
		Id:       cast.ToInt(param.Id),
		PageNum:  cast.ToInt(param.PageNum),
		PageSize: cast.ToInt(param.PageSize),
		Keyword:  param.Keyword,
	}

	result, err := salesmenService.SalesmenMerchantSalesmenList()
	if err != nil {
		appG.ResponseError(err.Error(), nil)
		return
	}

	appG.ResponseSuccess("成功", result)
}

type SalesmenMobileRegisterParam struct {
	Mobile interface{} `json:"mobile" `      //业务员手机号码
	Code   interface{} `json:"codeService" ` //验证码
}

// SalesmenMobileRegister 业务员注册
func SalesmenMobileRegister(c *gin.Context) {
	appG := app.Gin{C: c}
	var (
		param   SalesmenMobileRegisterParam
		errCode int
	)

	_, errCode = app.BindJsonAndValid(c, &param)
	if errCode != e.SUCCESS {
		appG.ResponseError(e.CodeMap[errCode], nil)
		return
	}

	if len(cast.ToString(param.Mobile)) < 4 {
		appG.ResponseError("电话号码长度不足四位", nil)
		return
	}

	userId, _ := c.Get("userId")

	codeService := codeService.Code{
		Code:   param.Code,
		Mobile: param.Mobile,
	}

	//验证手机验证码是否正确
	exist, err := codeService.ExistCodeByMobile()
	if err != nil || !exist {
		appG.ResponseError(err.Error(), nil)
		return
	}

	salesmenService := salesmenService.Salesmen{
		Name:    cast.ToString(param.Mobile)[len(cast.ToString(param.Mobile))-4:],
		Mobile:  param.Mobile,
		AgentId: cast.ToString(userId),
	}

	err = salesmenService.SalesmenRegister()
	codeService.UpdateCodeIsCheck()
	if err != nil {
		appG.ResponseError(err.Error(), nil)
		return
	}

	appG.ResponseSuccess("添加成功", nil)
}
